package editorsvg.editor.herramientas;

import editorsvg.editor.cursores.Cursores;
import editorsvg.editor.formas.Linea;
import editorsvg.editor.vista.EditorDocumento;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

/**
 * Herramienta para la insercción de rectangulos.
 */
public class HerramientaLinea extends HerramientaBase {
    private EditorDocumento editor;
    private Linea linea;
    private Point2D posicionBase;
 
    public HerramientaLinea(EditorDocumento editor) {
        this.editor = editor;        
    }

    @Override
    public boolean pulsado(MouseEvent e) {
        Point2D posicion = editor.getConversor().aUnidadesReales(e.getPoint());
        if (posicionBase == null) {
            this.linea = new Linea();
            posicionBase = posicion;
            linea.setPosicion1(posicion);
            linea.setPosicion2(posicion);
        } else if (posicionBase.equals(posicion)) {
            return false;
        } else {
            linea.setPosicion2(posicion);
            editor.finalizarCreacionForma(linea);
            posicionBase = null;
            linea = null;
        }
        return true;        
    }

    @Override
    public boolean moviendo(MouseEvent e) {
        if (linea != null) {
            Point2D posicion = editor.getConversor().aUnidadesReales(e.getPoint());
            linea.setPosicion2(posicion);
            System.out.println("posicion: " + posicion);
            return true;
        }
        return false;
    }

    @Override
    public Cursor getCursor() {
        return Cursores.LINEA.getCursor();
    }

    @Override
    public void pintarEnPixeles(Graphics2D graphics) {              
        if (linea == null) {
            return;
        }
        graphics.setColor(Color.BLACK);            
        graphics.setXORMode(Color.WHITE);
        graphics.setStroke(new BasicStroke(1));
        
        Point p1 = editor.getConversor().aPixeles(linea.getPosicion1());
        Point p2 = editor.getConversor().aPixeles(linea.getPosicion2());
        graphics.draw(new Line2D.Double(p1, p2));
//        Shape formaPixels = editor.getConversor().transformarAPixeles(linea.getShape());
//        graphics.draw(formaPixels);
        
        graphics.setPaintMode();
    }

}
